#ifndef __FLASH_H__
#define __FLASH_H__

#include "vbb_SPI.h"
#include "vbb_Utils.h"

#define CMD_WREN    0x06
#define CMD_RDSR    0x05
#define CMD_READ    0x03 // 3-byte address
#define CMD_SE      0xD8 // 3-byte address
#define CMD_PP      0x02 // 3-byte address

//#define CMD_READ    0x13 // 4-byte address
//#define CMD_SE      0xDC // 4-byte address
//#define CMD_PP      0x12 // 4-byte address

#define WEL         0x2
#define WIP         0x1

#ifdef CONFIG_STM32G_MAINTAIN
#define FLASH_PAGE_BUF_LEN 512
#endif
typedef struct
{
    VBB_SPI spi;
    VBB_Pin select_pin;
    uint32_t pageSize;
    uint32_t numOfPages;
    uint8_t last_status;
    char tmpPage[FLASH_PAGE_BUF_LEN];
} VBB_Flash;

void vbb_Flash_Init(VBB_Flash * Flash, uint32_t PageSize, uint32_t NumOfPages, SPI_TypeDef * SPIx, GPIO_TypeDef * GPIOx, uint32_t SelectPin);
void vbb_Flash_Select(VBB_Flash * Flash);
void vbb_Flash_Deselect(VBB_Flash * Flash);
void vbb_Flash_Wait(VBB_Flash * Flash, uint32_t Flag);
void vbb_Flash_EnableWrite(VBB_Flash * Flash);
void vbb_Flash_EraseSector(VBB_Flash * Flash, uint32_t Address);
void vbb_Flash_PageProgram(VBB_Flash * Flash, uint32_t Address);
void vbb_Flash_PageRead(VBB_Flash * Flash, uint32_t Address);
void vbb_Flash_EnableExtAddr(VBB_Flash * Flash);
char vbb_Flash_IsEnableExtAddr(VBB_Flash * Flash);
char vbb_Flash_ReadId(VBB_Flash * Flash);
char vbb_Flash_ReadElectronicSignature(VBB_Flash * Flash);
void vbb_Flash_UnlockFlash(VBB_Flash * Flash);

#endif
